package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.zltx.bean.QsApplyUpdateRecord;
import com.jy.adscm.zltx.bean.QsApprovalRecord;
import com.jy.adscm.zltx.bean.QsFile;
import com.jy.adscm.zltx.bean.QsQsfileDept;
import com.jy.adscm.zltx.mapper.mysql.*;
import com.jy.adscm.zltx.service.QsApplyUpdateRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 文件修改申请记录 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-10-23
 */
@Service
public class QsApplyUpdateRecordServiceImpl extends ServiceImpl<QsApplyUpdateRecordMapper, QsApplyUpdateRecord> implements QsApplyUpdateRecordService {
    @Autowired
    QsApplyUpdateRecordMapper qsApplyUpdateRecordMapper;
    @Autowired
    QsFileContentMapper QsFileContentMapper;
    @Autowired
    QsFileMapper qsFileMapper;
    @Autowired
    QsQsfileDeptMapper QsQsfileDeptMapper;
    @Autowired
    QsApprovalRecordMapper qsApprovalRecordMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public List<QsApplyUpdateRecord> selectByFileId(Integer fileId) {
        Wrapper<QsApplyUpdateRecord> wrapper =new EntityWrapper<>();
        wrapper.eq("fileId",fileId);
        List<QsApplyUpdateRecord> qsApplyUpdateRecords = qsApplyUpdateRecordMapper.selectList(wrapper);
        for (QsApplyUpdateRecord qsApplyUpdateRecord : qsApplyUpdateRecords) {
            if ( qsApplyUpdateRecord.getApplicant()!=null&&!"".equals(qsApplyUpdateRecord.getApplicant())){
                JyUser jyUser = jyUserMapper.selectById(qsApplyUpdateRecord.getApplicant());
                qsApplyUpdateRecord.setApplicantName(jyUser.getName());
            }

        }
        return qsApplyUpdateRecords ;
    }
    @Transactional
    @Override
    public boolean updateById(QsApplyUpdateRecord qsApplyUpdateRecord){
       /*   if ( "0".equals(qsApplyUpdateRecord.getApprovalType()) && "1".equals(qsApplyUpdateRecord.getIsHrApprovl())&&
                  "1".equals(qsApplyUpdateRecord.getIsApprovalDept())&&
                  "1".equals(qsApplyUpdateRecord.getIsDeptMangerApproval())
          ){
              Wrapper<QsFileContent> wrapper = new EntityWrapper<>();
              wrapper.eq("fileId",qsApplyUpdateRecord.getFileId());
              wrapper.eq("contentType",qsApplyUpdateRecord.getContentType());
              List<QsFileContent> qsFileContents = QsFileContentMapper.selectList(wrapper);
              if (qsFileContents.size()>0){
                  QsFileContent qsFileContent = qsFileContents.get(0);
                  if (qsFileContent.getIsLocked()!=null){
                      qsFileContent.setIsLocked(0);
                      QsFileContentMapper.updateById(qsFileContent);
                      QsFile qsFile = qsFileMapper.selectById(qsApplyUpdateRecord.getFileId());
                      if ("1".equals(qsFile.getIsApproval())){
                          qsFile.setIsApproval("0");
                      }
                      qsFileMapper.updateByQsFile(qsFile);
                      QsFileContentMapper.updateById(qsFileContent);
                  }else {
                      new AdscmSystemException("该内容未被审核，可以直接修改");
                  }
              }
          }
        if ( "1".equals(qsApplyUpdateRecord.getApprovalType()) &&
                "1".equals(qsApplyUpdateRecord.getIsDeptMangerApproval())
        ){
            Wrapper<QsFileContent> wrapper = new EntityWrapper<>();
            wrapper.eq("fileId",qsApplyUpdateRecord.getFileId());
            wrapper.eq("contentType",qsApplyUpdateRecord.getContentType());
            List<QsFileContent> qsFileContents = QsFileContentMapper.selectList(wrapper);
            if (qsFileContents.size()>0){
                QsFileContent qsFileContent = qsFileContents.get(0);
                if (qsFileContent.getIsLocked()!=null){
                    qsFileContent.setIsLocked(0);
                    QsFileContentMapper.updateById(qsFileContent);
                    QsFile qsFile = qsFileMapper.selectById(qsApplyUpdateRecord.getFileId());
                        if ("1".equals(qsFile.getIsApproval())){
                            qsFile.setIsApproval("0");
                        }
                    qsFileMapper.updateByQsFile(qsFile);
                }else {
                    new AdscmSystemException("该内容未被审核，可以直接修改");
                }
            }
        }*/
        return super.updateById(qsApplyUpdateRecord);
    }
    @Override
    @Transactional
   public  boolean insertQsApplyUpdateRecord(QsApplyUpdateRecord qsApplyUpdateRecord)throws AdscmSystemException{
        //查询qsFile如果没发布不能新增
        QsFile qsFile = qsFileMapper.selectById(qsApplyUpdateRecord.getFileId());
        if ("0".equals(qsFile.getIsApproval())){
            throw  new AdscmSystemException("不能新增版本未发布");
        }

        //先查询如果有审核记录没完成的不能新增
    
        //根据fileId查询相应的审核部门
        boolean insert = super.insert(qsApplyUpdateRecord);
        Wrapper<QsQsfileDept> wrapper=new EntityWrapper<>();
        wrapper.eq("fileId",qsApplyUpdateRecord.getFileId());
        List<QsQsfileDept> qsQsfileDepts = QsQsfileDeptMapper.selectList(wrapper);
        if (qsQsfileDepts.size()>0){
            for (QsQsfileDept qsQsfileDept : qsQsfileDepts) {
                QsApprovalRecord qsApprovalRecord=new QsApprovalRecord();
                qsApprovalRecord.setFileId(qsApplyUpdateRecord.getFileId());
                qsApprovalRecord.setUpdateId(qsApplyUpdateRecord.getId());
                qsApprovalRecord.setDeptId(qsQsfileDept.getDeptId().toString());
                qsApprovalRecordMapper.insert(qsApprovalRecord);
            }
        }else {
            throw  new AdscmSystemException("没有找到相应的审核部门");
        }

        return insert;
   }
}
